<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Admin_user extends MY_Controller {

    function __construct() {
        parent::__construct();
        $cek_admin = $this->session->userdata('cek_admin');
        if (!$cek_admin)
            redirect("admin");

        $this->muser = new M_user();
    }

    function index() {

        $data['title'] = 'Data user Pengguna';
        $data['list'] = $this->muser->get();
        $data['isi'] = 'isi';
        $this->load->view('template_admin', $data);
    }

    function tambah() {

        $data['title'] = 'Tambah User Pengguna';
        $data['id'] = "";
        $data['nama_user'] = "";
        $data['akses'] = "1";
        $data['status'] = "1";

        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function edit($id = NULL) {

        $data['title'] = 'Ubah User Pengguna';
        $dt = $this->muser->where("id = '$id'")->get();

        $data['id'] = $id;
        $data['nama_user'] = $dt->nama_user;
        $data['akses'] = $dt->akses;
        $data['status'] = $dt->status;

        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function edit_password($id = NULL, $error = NULL) {
        $data['error'] = $error;
        $data['title'] = 'Ubah Password Pengguna';
        $dt = $this->muser->where("id = '$id'")->get();
        $data['id'] = $id;
        $data['isi'] = 'form_password';
        $this->load->view('template_admin', $data);
    }

    function delete($id) {
        $this->muser->delete($id);
        redirect("admin_user");
    }

    function simpan() {
        $id = $this->input->post('id');
        if ($id == "") {
            $data = array(
                'nama_user' => $this->input->post('nama_user'),
                'password' => md5($this->input->post('password')),
                'akses' => $this->input->post('akses'),
                'status' => $this->input->post('status'),
            );
            $this->muser->insert($data);
        } else {
            $data = array(
                'nama_user' => $this->input->post('nama_user'),
                'akses' => $this->input->post('akses'),
                'status' => $this->input->post('status'),
            );
            $this->muser->update($data, $id);
        }

        redirect("admin_user");
    }

    function simpan_password() {
        $id = $this->input->post('id');
        $old_password = md5($this->input->post('old_password'));

        $cek = $this->muser->where("password = '$old_password' and id = '$id'")->count();

        if ($cek == 1) {
            $data = array(
                'password' => md5($this->input->post('password'))
            );
            $this->muser->update($data, $id);
            redirect("admin_user");
        } else {
            $this->edit_password($id, "Password lama tidak sesuai !!!");
        }
    }

}

?>
